package fhq.hcmute.edu.vn.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import fhq.hcmute.edu.vn.languages.Language;
import fhq.hcmute.edu.vn.util.BundleUtil;

public class WiFiConnectorActivity extends Activity {

	public static final int WIFI_CONNECTOR_RESULT_CODE_OK = 1;
	public static final int WIFI_CONNECTOR_RESULT_CODE_CANCEL = 2;
	public static final String WIFI_PASSWORD = "WIFI_PASSWORD";

	private TextView txtWifiName;
	private TextView txtSecurity;
	private TextView txtSignalStrength;
	private CheckBox cbShowPassword;
	private EditText edtPassword;
	private Button btnOK;
	private Button btnCancel;

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Language.updateLanguageSystem(getBaseContext());
		setContentView(R.layout.activity_wifi_connector);
		inti();

	}

	private void inti() {
		txtWifiName = (TextView) findViewById(R.id.lblWiFiName);
		txtSecurity = (TextView) findViewById(R.id.lblSecurity);
		txtSignalStrength = (TextView) findViewById(R.id.lblSignalStrength);
		cbShowPassword = (CheckBox) findViewById(R.id.cbShowPassword);
		edtPassword = (EditText) findViewById(R.id.edtPassword);
		btnOK = (Button) findViewById(R.id.btnOK);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		// get information from intent
		Bundle bundle =
				getIntent().getBundleExtra(WifiManagerActivity.class.getName());
		String wifiName = bundle.getString(BundleUtil.WIFI_NAME);
		String wifiSignalStrength =
				bundle.getString(BundleUtil.WIFI_SIGNAL_STRENGTH);
		String wifiSecurity = bundle.getString(BundleUtil.WIFI_SECURITY_TYPE);
		txtWifiName.setText(wifiName);
		txtSecurity.setText(wifiSecurity);
		txtSignalStrength.setText(wifiSignalStrength);
		// add checkbox event
		cbShowPassword.setOnCheckedChangeListener(onChangedShowPassword);
		btnOK.setOnClickListener(onOK);
		btnCancel.setOnClickListener(onCancel);
	}

	private OnClickListener onOK = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString(BundleUtil.WIFI_PASSWORD, edtPassword
					.getText().toString());
			intent.putExtras(bundle);
			setResult(WIFI_CONNECTOR_RESULT_CODE_OK, intent);
			finish();
		}
	};
	private OnClickListener onCancel = new OnClickListener() {

		@Override
		public void onClick(View v) {
			setResult(WIFI_CONNECTOR_RESULT_CODE_CANCEL);
			finish();
		}
	};
	private OnCheckedChangeListener onChangedShowPassword =
			new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					if (buttonView.getId() == R.id.cbShowPassword) {
						if (isChecked) {
							edtPassword
									.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
							edtPassword.setSelection(edtPassword.getText()
									.length());
						} else {
							edtPassword.setInputType(InputType.TYPE_CLASS_TEXT
									| InputType.TYPE_TEXT_VARIATION_PASSWORD);
							edtPassword.setSelection(edtPassword.getText()
									.length());
						}
					}
				}
			};
}
